package cn.fl.product.service.impl;

import cmm.comm.facade.framework.api.ISysCdMapApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.comm.facade.framework.dto.SysCdMapDDTO;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.product.service.IMfrInfoService;
import cn.fl.product.service.ISplInfoService;
import cn.fl.product.transform.SplFlowSHVOTransform;
import cn.fl.product.transform.SplInfoVOTransform;
import cn.fl.product.vo.MfrInfoVO;
import cn.fl.product.vo.SplFlowSHVO;
import cn.fl.product.vo.SplInfoVO;
import cn.fl.spl.transform.SplLimRateVOTransform;
import cn.fl.spl.vo.SplLimRateVO;
import cn.fl.system.transform.SysCdMapDVOTransform;
import cn.fl.system.transform.SysUlFilesRecVOTransform;
import cn.fl.system.vo.SysCdMapDVO;
import cn.fl.system.vo.SysUlFilesRecVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.mfr.condition.MfrInfoQueryCondition;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplInfoQueryCondition;
import fl.spl.facade.spl.constant.SplInfoConstant;
import fl.spl.facade.spl.dto.SplInfoDTO;
import fl.spl.facade.spllim.api.ISplLimRateApiService;
import fl.spl.facade.spllim.condition.SplLimRateQC;
import fl.spl.facade.spllim.dto.SplLimRateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 供应商信息表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class SplInfoServiceImpl implements ISplInfoService {

	/**
	 * 供应商 API 暴露服务
	 */
	@Autowired
	private ISplInfoApiService splInfoApiService;

	/**
	 * 映射表API服务
	 */
	@Autowired
	private ISysCdMapApiService sysCdMapApiService;

	/**
	 * 组织架构API暴露服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;

	/**
	 * 厂商服务
	 */
	@Autowired
	private IMfrInfoService prdMfrInfoService;

	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 用户服务
	 */
	@Autowired
	private ISysUsrApiService sysUsrApiService;

	@Autowired
	private ISplLimRateApiService splLimRateApiService;

	@Override
	public SplInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("根据主键查询供应商信息失败,主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplInfoDTO dto = this.splInfoApiService.selectPrdSplInfonById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		SplInfoVO prdSplInfoVO = SplInfoVOTransform.toVO(dto);
		if(null != prdSplInfoVO){
			this.setPrdSplInfoParams(prdSplInfoVO,null);
		}
		return prdSplInfoVO;
	}

	@Override
	public Long add(SplInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增供应商失败,供应商对象参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplInfoDTO dto = SplInfoVOTransform.toDTO(entity);
		BaseLogUtils.println("====================================");
		return this.splInfoApiService.addPrdSplInfo(dto,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),SplSystemConstant.FL_WEB_CHL_CD);
	}

	/*@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		return this.prdSplInfoApiService.removeById(id);
	}*/

	@Override
	public boolean modify(SplInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改供应商失败,供应商对象参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splInfoApiService.modifyPrdSplInfo(SplInfoVOTransform.toDTO(entity),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
	}

	@Override
	public boolean modifySplInfo(SplInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改供应商失败,供应商对象参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splInfoApiService.modifySplInfo(SplInfoVOTransform.toDTO(entity),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}


	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		page = this.splInfoApiService.searchPrdSplInfoListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<SplInfoDTO> resultList = (List<SplInfoDTO>) page.getResultList();
		List<SplInfoVO> voList = SplInfoVOTransform.toVOList(resultList);
		List<Long> crtUsrIdList = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(voList)){
			List<Long> idList = new ArrayList<>(voList.size());
			//获取经销商来源渠道CODE-渠道名称MAP
			Map<String, String> splSourceChlCdAndNmMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), fl.spl.facade.framework.constant.DictionaryConstant.SYS_CHANNEL_CODE_DIC_CD);
			//经销商归属CODE-归属名称 MAP
			Map<String, String> splOwnerCdAndNmMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), fl.spl.facade.framework.constant.DictionaryConstant.SYS_SPL_OWNER_CODE_DIC_CD);
			for (SplInfoVO prdSplInfoVO : voList) {
				//设置提报人姓名
				Long crtUsrId = prdSplInfoVO.getCrtUsrId();
				if(null!=crtUsrId){
					crtUsrIdList.add(crtUsrId);
				}
				if(CollectionUtils.isMapNotEmpty(splSourceChlCdAndNmMap) && splSourceChlCdAndNmMap.containsKey(prdSplInfoVO.getSplSource())){
					prdSplInfoVO.setSplSourceNm(splSourceChlCdAndNmMap.get(prdSplInfoVO.getSplSource()));
				}
				if(CollectionUtils.isMapNotEmpty(splOwnerCdAndNmMap) && splOwnerCdAndNmMap.containsKey(prdSplInfoVO.getOwnerCd())){
					prdSplInfoVO.setOwnerCdNm(splOwnerCdAndNmMap.get(prdSplInfoVO.getOwnerCd()));
				}
				idList.add(prdSplInfoVO.getId());
			}
			if(CollectionUtils.isNotEmpty(crtUsrIdList)){
				SysUsrQueryCondition suQC = new SysUsrQueryCondition();
				suQC.setUsrIds(crtUsrIdList);
				suQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
				List<SysUsrDTO> sysUsrDTOList = this.sysUsrApiService.searchSysUsrList(suQC);
				if(CollectionUtils.isNotEmpty(sysUsrDTOList)){
					for (SplInfoVO prdSplInfoVO : voList) {
						for (SysUsrDTO sysUsrDTO : sysUsrDTOList) {
							if(null!=sysUsrDTO && null!=prdSplInfoVO && null!=sysUsrDTO.getId()
									&& sysUsrDTO.getId().equals(prdSplInfoVO.getCrtUsrId())){
								prdSplInfoVO.setCrtUsrNm(sysUsrDTO.getRefRlNm());
							}
						}
					}
				}

			}
			if (CollectionUtils.isNotEmpty(idList)) {
				//查询周转化率
				SplLimRateQC limRateQC = new SplLimRateQC();
				limRateQC.setDataIdList(idList);
				limRateQC.setTabNm(SplInfoConstant.PRD_SPL_TAB_NAME);
				limRateQC.setIsDel(BaseConstant.IS_YESNO_NO);
				limRateQC.setIsNew(BaseConstant.IS_YESNO_YES);
				List<SplLimRateDTO> limRateDTOList = this.splLimRateApiService.searchList(limRateQC);
				if (CollectionUtils.isNotEmpty(limRateDTOList)) {
					Map<Long, SplLimRateDTO> limRateMap = new HashMap<>(limRateDTOList.size());
					for (SplLimRateDTO limRateDTO : limRateDTOList) {
						limRateMap.put(limRateDTO.getDataId(), limRateDTO);
					}
					for (SplInfoVO splInfoVO : voList) {
						if (limRateMap.containsKey(splInfoVO.getId())) {
							Double slewRate = limRateMap.get(splInfoVO.getId()).getSlewRate();
							splInfoVO.setSplSlewRate(DecimalUtils.mul(slewRate, 100));
						}
					}
				}
			}
			splSourceChlCdAndNmMap = null ;
			splOwnerCdAndNmMap = null ;
		}
		page.setResultList(voList);
		return page;
	}

	@Override
	public Pager searchListPageWithEncrypt(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.splInfoApiService.searchPrdSplInfoListPageWithEncrypt(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<SplInfoDTO> resultList = (List<SplInfoDTO>) page.getResultList();
		List<SplInfoVO> voList = SplInfoVOTransform.toVOList(resultList);
		List<Long> crtUsrIdList = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(voList)){
			List<Long> idList = new ArrayList<>(voList.size());
			//获取经销商来源渠道CODE-渠道名称MAP
			Map<String, String> splSourceChlCdAndNmMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), fl.spl.facade.framework.constant.DictionaryConstant.SYS_CHANNEL_CODE_DIC_CD);
			//经销商归属CODE-归属名称 MAP
			Map<String, String> splOwnerCdAndNmMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), fl.spl.facade.framework.constant.DictionaryConstant.SYS_SPL_OWNER_CODE_DIC_CD);
			for (SplInfoVO prdSplInfoVO : voList) {
				//设置提报人姓名
				Long crtUsrId = prdSplInfoVO.getCrtUsrId();
				if(null!=crtUsrId){
					crtUsrIdList.add(crtUsrId);
				}
				if(CollectionUtils.isMapNotEmpty(splSourceChlCdAndNmMap) && splSourceChlCdAndNmMap.containsKey(prdSplInfoVO.getSplSource())){
					prdSplInfoVO.setSplSourceNm(splSourceChlCdAndNmMap.get(prdSplInfoVO.getSplSource()));
				}
				if(CollectionUtils.isMapNotEmpty(splOwnerCdAndNmMap) && splOwnerCdAndNmMap.containsKey(prdSplInfoVO.getOwnerCd())){
					prdSplInfoVO.setOwnerCdNm(splOwnerCdAndNmMap.get(prdSplInfoVO.getOwnerCd()));
				}
				idList.add(prdSplInfoVO.getId());
			}
			if(CollectionUtils.isNotEmpty(crtUsrIdList)){
				SysUsrQueryCondition suQC = new SysUsrQueryCondition();
				suQC.setUsrIds(crtUsrIdList);
				suQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
				List<SysUsrDTO> sysUsrDTOList = this.sysUsrApiService.searchSysUsrList(suQC);
				if(CollectionUtils.isNotEmpty(sysUsrDTOList)){
					for (SplInfoVO prdSplInfoVO : voList) {
						for (SysUsrDTO sysUsrDTO : sysUsrDTOList) {
							if(null!=sysUsrDTO && null!=prdSplInfoVO && null!=sysUsrDTO.getId()
									&& sysUsrDTO.getId().equals(prdSplInfoVO.getCrtUsrId())){
								prdSplInfoVO.setCrtUsrNm(sysUsrDTO.getRefRlNm());
							}
						}
					}
				}

			}
			if (CollectionUtils.isNotEmpty(idList)) {
				//查询周转化率
				SplLimRateQC limRateQC = new SplLimRateQC();
				limRateQC.setDataIdList(idList);
				limRateQC.setTabNm(SplInfoConstant.PRD_SPL_TAB_NAME);
				limRateQC.setIsDel(BaseConstant.IS_YESNO_NO);
				limRateQC.setIsNew(BaseConstant.IS_YESNO_YES);
				List<SplLimRateDTO> limRateDTOList = this.splLimRateApiService.searchList(limRateQC);
				if (CollectionUtils.isNotEmpty(limRateDTOList)) {
					Map<Long, SplLimRateDTO> limRateMap = new HashMap<>(limRateDTOList.size());
					for (SplLimRateDTO limRateDTO : limRateDTOList) {
						limRateMap.put(limRateDTO.getDataId(), limRateDTO);
					}
					for (SplInfoVO splInfoVO : voList) {
						if (limRateMap.containsKey(splInfoVO.getId())) {
							Double slewRate = limRateMap.get(splInfoVO.getId()).getSlewRate();
							splInfoVO.setSplSlewRate(DecimalUtils.mul(slewRate, 100));
						}
					}
				}
			}
			splSourceChlCdAndNmMap = null ;
			splOwnerCdAndNmMap = null ;
		}
		page.setResultList(voList);
		return page;
	}

	@Override
	public List<KeyValueVO> searchMfrKVList() throws BaseException {
		MfrInfoQueryCondition qc = new MfrInfoQueryCondition();
		qc.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_PASS);//审核通过
		qc.setIsValid(BaseConstant.IS_YESNO_YES);//启用
		List<MfrInfoVO> prdMfrInfoVOList = this.prdMfrInfoService.searchList(qc);
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isNotEmpty(prdMfrInfoVOList)){
			for (MfrInfoVO prdMfrInfoVO : prdMfrInfoVOList) {
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(String.valueOf(prdMfrInfoVO.getId()));//厂商id
				kv.setValue(prdMfrInfoVO.getMfrName());//厂商名称
				kvList.add(kv);
			}
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchCapUntCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_REG_CAP_UNT_CD);
	}

	@Override
	public List<KeyValueVO> searchBrandList(String code) throws BaseException {
		return this.getKvList(code);
	}

	@Override
	public List<KeyValueVO> searchCorpTypCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_CORP_TYP_CD);
	}

	@Override
	public List<SysCdMapDVO> searchSysCdMapDList(SysCdMapDQueryCondition condition) throws BaseException {
		List<SysCdMapDDTO> listDTO = this.sysCdMapApiService.searchSysCdMapDWithTargetList(condition);
		return SysCdMapDVOTransform.toVOList(listDTO);
	}

	@Override
	public DataResultDTO addSplAdmissionFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.newWebLogger("用户ID和业务数据主键必须要存在!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001, "用户ID和业务数据主键必须要存在!");
		}
		return this.splInfoApiService.addSplAdmissionFlowStart(flowStartParamsDTO);
	}

	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newWebLogger("删除供应商失败,主键参数为").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splInfoApiService.removePrdSplInfoByIds(ids);
	}

	@Override
	public List<KeyValueVO> searchAudStsCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_AUD_STS_CD);
	}

	@Override
	public SplInfoVO checkSplExist(String splNm, String bizLicNo)
			throws BaseException {
		if(StringUtils.isBlank(splNm)){
			BaseLogUtils.newWebLogger("校验供应商是否已存在失败,供应商名称为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplInfoDTO prdSplInfoDTO = this.splInfoApiService.checkSplExist(splNm, bizLicNo,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentBuOrgCd());
		return SplInfoVOTransform.toVO(prdSplInfoDTO);
	}

	@Override
	public List<KeyValueVO> searchBuOrgCdList() throws BaseException {
		List<SysOrgDTO> dtoList = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isNotEmpty(dtoList)){
			for (SysOrgDTO dto : dtoList) {
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(dto.getOrgCd());
				kv.setValue(dto.getOrgDispNm());
				kvList.add(kv);
			}
		}
		return kvList;
	}



	@Override
	public List<KeyValueVO> searchPrjTypCdList() throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		String grpCd = DictionaryConstant.CODE_PRJ_TYPE_CD;
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), WebBaseConstant.SYS_P_ORG_CD, grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}

	@Override
	public boolean modifyAudStsCd(Long id, String audStsCd, Date mdfTm,
								  Long mdfUsrId) throws BaseException {
		if(StringUtils.isEmpty(id)
				||StringUtils.isEmpty(audStsCd)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splInfoApiService.modifySplInfoAudStsCd(id, audStsCd, mdfTm, mdfUsrId);
	}

	@Override
	public int updateBlackList(String id, String blackList) throws BaseException{
		if(StringUtils.isEmpty(id)
				||StringUtils.isEmpty(blackList)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		return splInfoApiService.updateBlackList(id,blackList);
	}

	@Override
	public boolean batchAddFileList(List<SysUlFilesRecVO> fileList)
			throws BaseException {
		if(CollectionUtils.isEmpty(fileList)){
			throw new BaseException(BaseExceptionConstant.COMMON_0001, BaseExceptionConstant.COMMON_0001_NAME);
		}
		return this.splInfoApiService.batchAddFileList(SysUlFilesRecVOTransform.toDTOList(fileList));
	}

	@Override
	public List<KeyValueVO> searchSplLvlCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_SPL_LVL_CD);
	}

	@Override
	public List<KeyValueVO> searchPrjLvlCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_SPL_PRJ_LVL_CD);
	}

	@Override
	public List<SplFlowSHVO> searchSplFlowList(Long splId) throws BaseException {

		return SplFlowSHVOTransform.toVOList(this.splInfoApiService.searchSplFlowList(splId,CurrentThreadContext.getCurrentSysCd()));
	}

	@Override
	public List<KeyValueVO> searchNewCarTyp() throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		String grpCd = WebBaseConstant.SPL_PROP_CODE_GRP_CD;
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(SplSystemConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, grpCd);
			BaseLogUtils.newWebLogger("数据字典数据信息").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setKeys("sysDictionaryDTOList", JSON.toJSONString(sysDictionaryDTOList))
					.info();
			//kvList = sysDictionaryApiService.getKvListWithAttr1AndAttr2All(grpCd, "", "100", CurrentThreadContext.getCurrentSysCd(), WebBaseConstant.SYS_P_ORG_CD);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					if("100".equals(sysDictionaryDTO.getCodeAttr2())){
						KeyValueVO keyValueVO = new KeyValueVO();
						keyValueVO.setKey(sysDictionaryDTO.getCode());
						keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
						kvList.add(keyValueVO);
					}
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchUsedCarTyp() throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		String grpCd = "SPL0039";
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(SplSystemConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					if("200".equals(sysDictionaryDTO.getCodeAttr2())){
						KeyValueVO keyValueVO = new KeyValueVO();
						keyValueVO.setKey(sysDictionaryDTO.getCode());
						keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
						kvList.add(keyValueVO);
					}
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}

	@Override
	public List<KeyValueVO> searchBizScopeTyp() throws BaseException {
		return this.sysDictionaryApiService.getKvList(WebBaseConstant.SPL_BIZ_SCOPE_GRP_CD, WebBaseConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD);
	}

	@Override
	public List<KeyValueVO> searchSplAttrCdList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_TYP_CD, WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD);
	}

	@Override
	public List<KeyValueVO> searchOwnerCdList() throws BaseException {
		return this.sysDictionaryApiService.getKvList(SplSystemConstant.SPL_OWNER_CODE, WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD);
	}

	/**
	 * 通过ID及页面来源查询经销商（区分详细及修改页面）
	 *
	 * @param id
	 * @param opTypCd
	 * @return
	 * @throws BaseException
	 */
	@Override
	public SplInfoVO selectById(Long id, String opTypCd) throws BaseException {
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("根据主键查询供应商信息失败,主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplInfoDTO dto = this.splInfoApiService.selectPrdSplInfonById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		SplInfoVO prdSplInfoVO = SplInfoVOTransform.toVO(dto);
		//SplInfoVO prdSplInfoVO = this.selectById(id);
		if(null != prdSplInfoVO){
			this.setPrdSplInfoParams(prdSplInfoVO,opTypCd);
		}
		return prdSplInfoVO;
	}

	/**
	 * 通过ID及页面来源查询经销商（区分详细及修改页面）
	 *
	 * @param id
	 * @param opTypCd
	 * @return
	 * @throws BaseException
	 */
	@Override
	public SplInfoVO selectByIdWithEncrypt(Long id, String opTypCd) throws BaseException {
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("根据主键查询供应商信息失败,主键参数为空").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		}
		SplInfoDTO dto = this.splInfoApiService.selectPrdSplInfonByIdWithEncrypt(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		SplInfoVO prdSplInfoVO = SplInfoVOTransform.toVO(dto);
		//SplInfoVO prdSplInfoVO = this.selectById(id);
		if(null != prdSplInfoVO){
			this.setPrdSplInfoParams(prdSplInfoVO,opTypCd);
		}
		return prdSplInfoVO;
	}


	private SplInfoVO setPrdSplInfoParams(SplInfoVO prdSplInfoVO , String opTypCd){

		StringBuffer newCarCdNms = new StringBuffer();
		StringBuffer usedCarCdNms = new StringBuffer();
		StringBuffer bizScopeCdNms = new StringBuffer();

		//获取经销商类型
		Map<String, String> kyMap = sysDictionaryApiService.searchGroupToMap(SplSystemConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, WebBaseConstant.SPL_PROP_CODE_GRP_CD);

		//获取供应商经营范围
		Map<String, String> bizScopeMap = sysDictionaryApiService.searchGroupToMap(WebBaseConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, WebBaseConstant.SPL_BIZ_SCOPE_GRP_CD);

		//设置厂商字符串信息
		prdSplInfoVO.setMfrIdsString(this.listTransferToString(prdSplInfoVO.getMfrIds()));
		//设置卡车品牌
		prdSplInfoVO.setTkBrCdsString(this.listTransferToString(prdSplInfoVO.getTkBrCdList()));
		//设置乘用车品牌
		prdSplInfoVO.setCarBrCdsString(this.listTransferToString(prdSplInfoVO.getCarBrCdList()));
		//设置客车品牌
		prdSplInfoVO.setBusBrCdsString(this.listTransferToString(prdSplInfoVO.getBusBrCdList()));
		//设置事业部字符串信息
		prdSplInfoVO.setBuOrgCdsString(this.listTransferToString(prdSplInfoVO.getBuOrgCds()));
		//设置项目类型字符串信息
		prdSplInfoVO.setPrjTypCdsString(this.listTransferToString(prdSplInfoVO.getPrjTypCds()));
		//设置新车二手车类型
		List<String> chkBusTypCdList = prdSplInfoVO.getChkBusTypCdList();
		if(CollectionUtils.isNotEmpty(chkBusTypCdList)){
			ArrayList<String> newCarList = new ArrayList<>(chkBusTypCdList.size());
			ArrayList<String> usedCarList = new ArrayList<>(chkBusTypCdList.size());
			for (String string : chkBusTypCdList) {
				if(string.contains("2001")){
					usedCarList.add(string);
					usedCarCdNms.append(kyMap.get(string));
					usedCarCdNms.append(",");
				}else{
					newCarList.add(string);
					newCarCdNms.append(kyMap.get(string));
					newCarCdNms.append(",");
				}
			}
			if(newCarCdNms.length()>1){
				prdSplInfoVO.setNewCarCdNms(newCarCdNms.toString().substring(0,newCarCdNms.length()-1));
			}
			if(usedCarCdNms.length()>1){
				prdSplInfoVO.setUsedCarCdNms(usedCarCdNms.toString().substring(0,usedCarCdNms.length()-1));
			}
			if(CollectionUtils.isNotEmpty(newCarList)){
				String newCarString = this.listTransferToString(newCarList);
				prdSplInfoVO.setNewCarString(newCarString);
			}
			if(CollectionUtils.isNotEmpty(usedCarList)){
				String usedCarString = this.listTransferToString(usedCarList);
				prdSplInfoVO.setUsedCarString(usedCarString);
			}
		}
		//设置经营范围
		List<String> bizScopeCdList = prdSplInfoVO.getBizScopeCdList();
		CollectionUtils.removeDuplicate(bizScopeCdList);
		prdSplInfoVO.setBizScopeCdList(bizScopeCdList);
		if(CollectionUtils.isNotEmpty(bizScopeCdList)){
			StringBuilder bizScopeCdStr = new StringBuilder();
			//String bizScopeListStr = this.listTransferToString(bizScopeCdList);
			int i = 0 ;
			for (String bizScopeCd : bizScopeCdList) {
				if(bizScopeMap.containsKey(bizScopeCd)){
					bizScopeCdStr.append(bizScopeCd).append(";");
					bizScopeCdNms.append(bizScopeMap.get(bizScopeCd));
					bizScopeCdNms.append(",");
				}
				if(bizScopeCdStr.length() > 1 && i == bizScopeCdList.size() - 1){
					bizScopeCdStr.replace(bizScopeCdStr.length() - 1,bizScopeCdStr.length(),"");
				}
				i++;
			}
			if(bizScopeCdNms.length() >=1)
				bizScopeCdNms.replace(bizScopeCdNms.length() - 1,bizScopeCdNms.length(),"");
			if(StringUtils.isNotEmpty(opTypCd)){
				if(StringUtils.isNotEmpty(prdSplInfoVO.getBizScope())){
					if(bizScopeCdNms.length() > 1){
						bizScopeCdNms.append(",");
					}
					bizScopeCdNms.append(prdSplInfoVO.getBizScope());
				}
			}
			prdSplInfoVO.setBizScopeCdNms(bizScopeCdNms.toString());
			prdSplInfoVO.setBizScopeCdStr(bizScopeCdStr.toString());

		}else{
			if(StringUtils.isNotEmpty(opTypCd)){
				prdSplInfoVO.setBizScopeCdNms(prdSplInfoVO.getBizScope());
			}
		}
		if(StringUtils.isNotEmpty(prdSplInfoVO.getOwnerCd())){
			Map<String, String> ownerMap = sysDictionaryApiService.searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, SplSystemConstant.SPL_OWNER_CODE);
			prdSplInfoVO.setOwnerCdNm(ownerMap.get(prdSplInfoVO.getOwnerCd()));
		}
		return prdSplInfoVO ;
	}

	/**
	 * 设置页面字符串数据
	 * @param list
	 * @return
	 */
	private String listTransferToString(List<?> list){
		StringBuilder sb = new StringBuilder();
		if (CollectionUtils.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				if (i < list.size() - 1) {
					sb.append(list.get(i) + ";");
				} else {
					sb.append(list.get(i));
				}
			}
		}
		return sb.toString();
	}

	@Override
	public List<SplInfoVO> searchList(SplInfoQueryCondition condition) throws BaseException {
		// TODO Auto-generated method stub
		return SplInfoVOTransform.toVOList(this.splInfoApiService.searchSplInfoList(condition));
	}

	@Override
	public boolean modifyLoa(SplInfoVO vo) throws BaseException {
		// TODO Auto-generated method stub
		return this.splInfoApiService.modifySplLoa(SplInfoVOTransform.toDTO(vo),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public Pager searchSplLimRateListPage(Pager page) throws BaseException {
		Pager pager = this.splLimRateApiService.searchListPager(page);
		if (null != pager) {
			List<SplLimRateDTO> resultList = (List<SplLimRateDTO>) pager.getResultList();
			List<SplLimRateVO> voList = SplLimRateVOTransform.toVOList(resultList);
			if (CollectionUtils.isNotEmpty(voList)) {
				List<Long> idList = new ArrayList<>(voList.size());
				for (SplLimRateVO vo : voList) {
					idList.add(vo.getCrtUsrId());
				}
				List<SysUsrDTO> usrDTOList = this.sysUsrApiService.selectSysUserInfoList(idList);
				if (CollectionUtils.isNotEmpty(usrDTOList)) {
					Map<Long, String> usrMap = new HashMap<>(usrDTOList.size());
					for (SysUsrDTO sysUsrDTO : usrDTOList) {
						usrMap.put(sysUsrDTO.getId(), sysUsrDTO.getRefRlNm());
					}

					for (SplLimRateVO vo : voList) {
						vo.setCrtUsrNm(usrMap.get(vo.getCrtUsrId()));

						vo.setSlewRate(DecimalUtils.mul(vo.getSlewRate(), 100));
					}
				}
			}
			pager.setResultList(voList);
		}
		return pager;
	}

}